package com.sduept.nwld.dataserver.manager.etl;

import com.sduept.cim.model.core.Substation;
import com.sduept.cim.model.wires.ACLineSegment;
import com.sduept.core.dao.SuperDAO;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.bigdata.fault.common.ETLDAO;
import com.sduept.nwld.dataserver.util.CimUtils;
import com.sduept.utils.CharacterUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class EMSAClineETLManager {
	@Autowired
	private SuperDAO dao;
	@Autowired
	private ETLDAO eDao;
	@Autowired
	private CimManager cm;
	
	public  void receiveAndSaveAllData() {
		List<?> list = getAllSourceData();
		receiveAndSave(list);
	}
	public  void receiveAndSavePartData(String date) {
		List<?> list = getAllSourceData(date);
		receiveAndSave(list);
	}
	
	
	public List<?> getAllSourceData(){
		List <?>result = eDao.query("select cim_mrid,cim_name,cim_substation1,cim_substation2,cim_voltvalue from t_ems_aclinesegment where cim_voltvalue > 499 ");
		return result;
	}
	public List<?> getAllSourceData(String date) {
		String sql = " select cim_mrid,cim_name,cim_substation1,cim_substation2,cim_voltvalue from t_ems_aclinesegment "
				+ " where update_time=";
		List<?> result = eDao.query(sql + "'" + date + "'");
		return result;
	}
	public  void receiveAndSave(List<?> list) {
		ETLDataSave equipmentSave = new ETLDataSave(list);
		equipmentSave.saveToBean();
	}

	public class ETLDataSave{
		private List<?> list;
		private Map<String,Substation> cimStationMap=new HashMap<String,Substation>();//cim保护线路Map
		private Map<String,String> emsStationMap=new HashMap<String,String>();//ems保护线路Map
		private Map<String,ACLineSegment> cimAclineMap=new HashMap<String,ACLineSegment>();//ems保护线路Map
		
		public ETLDataSave(List<?> list) {
			super();
			this.list = list;
			init();
		}
		private void init(){
			String sql ="select mrid,name from t_ems_substation where volttype >499";
			List<?> queryList = eDao.query(sql);
			for(Object obj:queryList){
				Object[] objs=(Object[])obj;
				if(objs[0]!=null&&objs[1]!=null){
					emsStationMap.put(objs[0].toString(), CharacterUtil.qj2bj(objs[1].toString()));
				}
			}
			List<Substation> allStations = cm.getAllStations();
			for(Substation sub:allStations){
				if(StringUtils.isNotEmpty(sub.getName())){
					cimStationMap.put(CharacterUtil.qj2bj(sub.getName()), sub);
				}
			}
			List<ACLineSegment> allAclines = cm.getAllAclines();
			for(ACLineSegment acline:allAclines){
				if(StringUtils.isNotEmpty(acline.getName())){
					cimAclineMap.put(CharacterUtil.qj2bj(acline.getName()), acline);
				}
			}
		}
		public void saveToBean(){
			//select mrid,cim_aliasname,cim_substation1,cim_substation2,cim_voltvalue from t_ems_aclinesegment where volttype >499
			List<ACLineSegment> addAclines=new ArrayList<ACLineSegment>();
			for(Object obj:list){
				Object[] objs=(Object[])obj;
				//判断是否存在此线路
				if(objs[1]==null){
					continue;
				}else{
					ACLineSegment acLineSegment = cimAclineMap.get(CharacterUtil.qj2bj(objs[1].toString()));
					if(acLineSegment==null){
						boolean isexist=false;
						for(String s:cimAclineMap.keySet()){
							float f = CimUtils.getStandVoltagelevel(objs[4].toString());
							if((CimUtils.lineNameMatching(s, CharacterUtil.qj2bj(objs[1].toString()),false))&&(f==cimAclineMap.get(s).getStandVoltagelevel())){
								isexist=true;
								break;
							}
						}
						if(!isexist){
							ACLineSegment acline=new ACLineSegment();
							if(objs[0]!=null){
								acline.setId(objs[0].toString());
							}else{
								acline.setId(ObjectId.get().toString());
							}
							if(objs[1]!=null){
								acline.setName(objs[1].toString());
							}
							if(objs[2]!=null){
								String name = emsStationMap.get(objs[2].toString());
								if(StringUtils.isNotEmpty(name)){
									Substation substation = cimStationMap.get(name);
									if(substation!=null){
										acline.setStartST(substation.getId());
										acline.setStartStName(substation.getName());
									}
								}
							}
							if(objs[3]!=null){
								String name = emsStationMap.get(objs[3].toString());
								if(StringUtils.isNotEmpty(name)){
									Substation substation = cimStationMap.get(name);
									if(substation!=null){
										acline.setEndST(substation.getId());
										acline.setEndStName(substation.getName());
									}
								}
							}if(objs[4]!=null){
								acline.setActualVoltage(Float.parseFloat(objs[4].toString()));
								acline.setStandVoltagelevel(CimUtils.getStandVoltagelevel(acline.getActualVoltage()));
							}
							addAclines.add(acline);
						}
					}
				}
			}
			log.info("============="+addAclines.size());
//			for(ACLineSegment ac : addAclines){
//				//log.info(ac.getName());
//			}
			try {
				dao.persistentBatch(ACLineSegment.class, addAclines);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
			
	}

}
